package editors.gui;
import org.lwjgl.opengl.GL11;

import graphics.system.Color;
import graphics.system.GLTools;
import tools.Floatmath;
import tools.Point;
public class GuiSlider extends GuiObject {
	private String title;
	private float maxv;
	private float minv;
	private float value;
	private boolean intvalue;
	public GuiSlider(Point pos, float width, String title, float minv, float maxv, boolean intval) {
		this.rect = new Rect(pos.add(new Point(width / 2, -GuiSettings.LINEHEIGHT)), width, GuiSettings.LINEHEIGHT * 2);
		this.title = title;
		this.minv = minv;
		this.maxv = maxv;
		intvalue = intval;
		value = 0;
	}
	public GuiSlider(Point pos, float width, String title, float minv, float maxv) {
		this(pos, width, title, minv, maxv, false);
	}
	public GuiSlider(Point pos, float width, String title, float minv, float maxv, float startv) {
		this(pos, width, title, minv, maxv, false);
		setValue(startv);
	}
	public GuiSlider(Point pos, float width, String title, float minv, float maxv, float startv, boolean intval) {
		this(pos, width, title, minv, maxv, intval);
		setValue(startv);
	}
	public void setValue(float startv) {
		value = ((startv - minv) / (maxv - minv));
	}
	@Override
	public boolean isClicked() {
		return clicked;
	}
	@Override
	public boolean checkClick(Point mp) {
		this.clicked = rect.isIn(mp);
		if (clicked)
			value = (mp.x - rect.getTopLeft().x) / rect.getWidth();
		return clicked;
	}
	@Override
	public void setClick(Point mp) {
		if (rect.isIn(mp))
			value = (mp.x - rect.getTopLeft().x) / rect.getWidth();
	}
	@Override
	public void draw() {
		rect.draw(GuiSettings.BUTTON_OFF_COL);
		Point s = rect.getTopLeft();
		s.sAdd(new Point(2, -GuiSettings.FONTADJUST));
		GLTools.drawText(s, title, GuiSettings.TEXT_COL, GuiSettings.FONTSIZE);
		s.sAdd(new Point(rect.getWidth() / 2 - GuiSettings.LINEHEIGHT * 2, -GuiSettings.LINEHEIGHT));
		String v = intvalue ? String.valueOf((int) this.calcValue()) : String.valueOf(this.calcValue());
		GLTools.drawText(s, v, GuiSettings.TEXT_COL, GuiSettings.FONTSIZE - 0.1f);
		GLTools.drawRect(rect.getTopLeft().add(new Point(value * rect.getWidth(), -GuiSettings.LINEHEIGHT * 1.5f)), 5, GuiSettings.LINEHEIGHT / 2, 0, Color.blend(Color.green, Color.red, value));
	}
	public float calcValue() {
		if (intvalue)
			return (float) ((int) (minv + (maxv - minv) * value));
		return minv + (maxv - minv) * value;
	}
}
